#include "Protocol.hpp"
#include "Socket.hpp"
#include "InetAddr.hpp"
#include "common.hpp"

void GetDataFromStdin(int* x,int* y,char* opre)
{
    std::cout << "please enter x:";
    std::cin >> *x;
    std::cout << "please enter y:";
    std::cin >> *y;
    std::cout << "please enter oper:";
    std::cin >> *opre;
}



int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        exit(1);
    }

    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();

    client->BulidClientSocketMethod();

    if(!client->Connect(ip,port))
    {
        exit(2);
    }

    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();
    std::string resp_buffer;
    while(true)
    {
        int x, y;
        char oper;
        GetDataFromStdin(&x,&y,&oper);

        std::string req_str = protocol->BuildRequestString(x,y,oper);

        //发送

        client->Send(req_str);

        //获取应答
        Response resp;
        bool res = protocol->GetResponse(client,resp_buffer,&resp);

        resp.ShowResult();
    }
    client->Close();
    return 0;
}